﻿#region Description
/*  File Name:      MEFPSComponent.cs
 *  Author:         Adam Goodchild
 *  
 *  Description:    This is an Frames Per Second Game Component
 *                  This class can be used to display the FPS for the Engine
*/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#endregion

namespace MinimizeEngine.Engine_Components
{
    public class MEFPS : GameComponent
    {
        #region Fields

        private float mFPS = 0.0f;
        private float mUpdateInterval = 1.0f;
        private float mTimeSinceLastUpdate = 0.0f;
        private float mFrameCount = 0;

        #endregion

        #region Properties

        /// <summary>
        /// Get the Current FPS
        /// </summary>
        public float FPS
        {
            get { return mFPS; }
        }

        public event EventHandler<EventArgs> Updated;

        #endregion

        #region Initialization

        public MEFPS(Game game) : base(game)
        {
            Enabled = true;
        }

        #endregion

        #region Update

        /// <summary>
        /// Update the FPS
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            
            //Get the Current Elapsed Real Game Time
            float mTimeElapsed = (float)gameTime.ElapsedRealTime.TotalSeconds;

            //Increase the Frame Count
            mFrameCount++;

            mTimeSinceLastUpdate += mTimeElapsed;

            //Calculate the FPS
            if (mTimeSinceLastUpdate > mUpdateInterval)
            {
                mFPS = mFrameCount / mTimeSinceLastUpdate;

                mFrameCount = 0;

                mTimeSinceLastUpdate -= mUpdateInterval;

                if(Updated != null)
                {
                    Updated(this, new EventArgs());
                }
            }
        }

        #endregion
    }
}
